export { clipboard } from './clipboard';
export { textSlice } from './textSlice';

export const countingProgressbar = (arr: Task[]) => {
  const filteredArrChecked = arr.filter((item) => !!item.is_closed).length;

  return (filteredArrChecked / arr.length) * 100;
};

export const firstCase = (userName: string): string => {
  return userName
    .split(' ')
    .map((item) => item.charAt(0))
    .join('');
};

type MoveElement = {
  arr: Task[];
  oldIndex: number;
  newIndex: number;
};
export const moveElement = ({ arr, oldIndex, newIndex }: MoveElement) => {
  const element = arr.splice(oldIndex, 1)[0];
  arr.splice(newIndex, 0, element);
  return arr;
};

type MoveElementColumn = {
  oldArr: Task[];
  newArr: Task[];
  oldIndex: number;
  newIndex: number;
  oldColIdx?: number;
  newColIdx?: number;
  newColId: number;
};
export const moveElementColumn = ({
  newArr,
  oldArr,
  oldIndex,
  newIndex,
  newColId
}: MoveElementColumn) => {
  const element = oldArr.splice(oldIndex, 1)[0];
  newArr.splice(newIndex, 0, element);
  const arr: Task[] = newArr.map((item) => {
    const newSubtasks = item.subtasks.map((subtask) => {
      const checklist = subtask.subtasks.map((check) => ({ ...check, column_id: newColId }));
      return { ...subtask, subtasks: checklist, column_id: newColId };
    });

    return { ...item, subtasks: newSubtasks, column_id: newColId };
  });
  return { oldArr, newArr: arr };
};

export const iterable = (arr: any[]) => {
  return arr?.map((item) => {
    return Object.assign({}, item, { selected: false, chosen: false });
  });
};
